using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using System.Threading;
using Services;
using SpaceBattle.Logic.SampleLevel;
using System.IO.IsolatedStorage;
using System.IO;
using SpaceBattle.Resources;
using Utilities.Menu.UIElements;


namespace SpaceBattle.Menu
{

  public class SkirmishOptionScreen : Microsoft.Xna.Framework.DrawableGameComponent
  {

    static readonly int MAX_ENEMIES = 7;
    static readonly int MAX_DIFFICULTY = 2;

    SpriteBatch sprite_batch;
    SpriteFont font32, font16, font24;
    Texture2D arrow, text_bg, confirm_v, bg;
    int enemies_n, difficulty;
    bool boss;
    Button back, confirm;
    Button[] op1_arr, op2_arr, op3_arr;
    Matrix screen_transform;

    float alpha;

    public SkirmishOptionScreen(Game game)
      : base(game)
    {
      enemies_n = 1;
      difficulty = 0;
      boss = false;

      alpha = 0.0f;
      this.UpdateOrder = (int)ComponentOrders.TRANSITION + 1;

      game.Components.Add(new InputManager(game));
    }

    public override void Initialize()
    {
      var input_service = Game.Services.GetService(typeof(IInputManager)) as IInputManager;
      input_service.OnTap += new InputManager.Tap(on_tap);

      sprite_batch = Game.Services.GetService(typeof(SpriteBatch)) as SpriteBatch;

      base.Initialize();
    }

    protected override void LoadContent()
    {
      font32 = Content.Load<SpriteFont>("Fonts/OCRAExtended32");
      font16 = Content.Load<SpriteFont>("Fonts/OCRAExtended16");
      font24 = Content.Load<SpriteFont>("Fonts/OCRAExtended24");

      bg = Content.Load<Texture2D>("Menu/bg");
      arrow = Content.Load<Texture2D>("Menu/SkirmOptM/arrow");
      text_bg = Content.Load<Texture2D>("Menu/title_bg");
      confirm_v = Content.Load<Texture2D>("Menu/confirm_box");

      back = new Button
      {
        Position = new Vector2(20, 710),
        Label = "",
        CollisionBox = new Rectangle(10, 700, confirm_v.Width + 20, confirm_v.Height + 20)
      };
      confirm = new Button
      {
        Position = new Vector2(330, 710),
        Label = "",
        CollisionBox = new Rectangle(320, 700, confirm_v.Width + 20, confirm_v.Height + 20)
      };
      op1_arr = CreateArrowButton(230);
      op2_arr = CreateArrowButton(370);
      op3_arr = CreateArrowButton(510);

      screen_transform = Matrix.CreateScale(GraphicsDevice.Viewport.Width / SpaceBattleGame.DEFAULT_SCR_WIDTH,
          GraphicsDevice.Viewport.Height / SpaceBattleGame.DEFAULT_SCR_HEIGHT, 1.0f);

      base.LoadContent();
    }

    protected override void Dispose(bool disposing)
    {

      base.Dispose(disposing);
    }

    void on_tap(Vector2 tap_position)
    {
      var p = tap_position.ToPoint(screen_transform);

      if (back.CollisionBox.Contains(p))
      {
        StateManager.SelectMenu(Game);
      }

      //else if (confirm.CollisionBox.Contains(p))
      //{
      //  StateManager.SelectSkirmishLevel(Game, enemies_n, difficulty, boss);
      //}

      else
      {
        for (int i = 0; i < op1_arr.Length; i++)
        {
          if (op1_arr[i].CollisionBox.Contains(p))
          {
            enemies_n = (int)MathHelper.Clamp(enemies_n + (i == 1 ? 1 : -1), 1, 7);
          }
        }

        for (int i = 0; i < op2_arr.Length; i++)
        {
          if (op2_arr[i].CollisionBox.Contains(p))
          {
            difficulty = (int)MathHelper.Clamp(difficulty + (i == 1 ? 1 : -1), 0, 2);
          }
        }

        for (int i = 0; i < op3_arr.Length; i++)
        {
          if (op3_arr[i].CollisionBox.Contains(p))
          {
            boss = i == 1 ? true : false;
          }
        }
      }

    }

    public override void Update(GameTime gameTime)
    {
      float dt = (float)gameTime.ElapsedGameTime.TotalSeconds;

      alpha = alpha >= 1.0f ? 1.0f : alpha + dt;

      base.Update(gameTime);
    }

    public override void Draw(GameTime gameTime)
    {
      GraphicsDevice.Clear(Color.Black);

      var w = sprite_batch.GraphicsDevice.Viewport.Width;
      var h = sprite_batch.GraphicsDevice.Viewport.Height;

      sprite_batch.Begin(SpriteSortMode.Deferred, null, null, null, null, null, screen_transform);

      sprite_batch.Draw(bg, Vector2.Zero, Color.White);

      sprite_batch.Draw(text_bg, new Vector2(w / 2.0f - text_bg.Width / 2.0f, 60), Color.White * alpha);

      sprite_batch.DrawString(font32, Strings.Skirmish_title,
          new Vector2(w / 2.0f - font32.MeasureString(Strings.Skirmish_title).X / 2.0f, 63 + (text_bg.Height - font32.MeasureString(Strings.Skirmish_title).Y) / 2.0f),
          Color.Black * alpha);
      sprite_batch.DrawString(font32, Strings.Skirmish_title,
          new Vector2(w / 2.0f - font32.MeasureString(Strings.Skirmish_title).X / 2.0f, 60 + (text_bg.Height - font32.MeasureString(Strings.Skirmish_title).Y) / 2.0f),
          Color.White * alpha);

      sprite_batch.Draw(text_bg, new Rectangle(w / 2 - text_bg.Width / 2, 180, text_bg.Width, text_bg.Height / 2), Color.White * alpha);
      sprite_batch.Draw(text_bg, new Rectangle(w / 2 - text_bg.Width / 4, (int)(op1_arr[0].Position.Y + (arrow.Height - text_bg.Height / 2.0f) / 2.0f), text_bg.Width / 2, text_bg.Height / 2), Color.White * alpha);

      sprite_batch.DrawString(font16, Strings.Skirmish_op1,
          new Vector2(w / 2.0f - font16.MeasureString(Strings.Skirmish_op1).X / 2.0f, 180 + (text_bg.Height / 2.0f - font16.MeasureString(Strings.Skirmish_op1).Y) / 2.0f),
          Color.White * alpha);

      sprite_batch.DrawString(font16, enemies_n.ToString(),
          new Vector2(w / 2.0f - font16.MeasureString(enemies_n.ToString()).X / 2.0f,
              (int)(op1_arr[0].Position.Y + (arrow.Height - text_bg.Height / 2.0f) / 2.0f) + (text_bg.Height / 2.0f - font16.MeasureString(enemies_n.ToString()).Y) / 2.0f),
          Color.White * alpha);

      sprite_batch.Draw(arrow, op1_arr[0].Position, Color.White * alpha);
      sprite_batch.Draw(arrow, op1_arr[1].Position, null, Color.White * alpha, 0, Vector2.Zero, 1, SpriteEffects.FlipHorizontally, 0);

      sprite_batch.Draw(text_bg, new Rectangle(w / 2 - text_bg.Width / 2, 330, text_bg.Width, text_bg.Height / 2), Color.White * alpha);
      sprite_batch.Draw(text_bg, new Rectangle(w / 2 - text_bg.Width / 4, (int)(op2_arr[0].Position.Y + (arrow.Height - text_bg.Height / 2.0f) / 2.0f), text_bg.Width / 2, text_bg.Height / 2), Color.White * alpha);

      sprite_batch.DrawString(font16, Strings.Skirmish_op2,
          new Vector2(w / 2.0f - font16.MeasureString(Strings.Skirmish_op2).X / 2.0f, 330 + (text_bg.Height / 2.0f - font16.MeasureString(Strings.Skirmish_op2).Y) / 2.0f),
          Color.White * alpha);

      sprite_batch.DrawString(font16, getDifficult(),
          new Vector2(w / 2.0f - font16.MeasureString(getDifficult()).X / 2.0f,
              (int)(op2_arr[0].Position.Y + (arrow.Height - text_bg.Height / 2.0f) / 2.0f) + (text_bg.Height / 2.0f - font16.MeasureString(getDifficult()).Y) / 2.0f),
          Color.White * alpha);

      sprite_batch.Draw(arrow, op2_arr[0].Position, Color.White * alpha);
      sprite_batch.Draw(arrow, op2_arr[1].Position, null, Color.White * alpha, 0, Vector2.Zero, 1, SpriteEffects.FlipHorizontally, 0);

      sprite_batch.Draw(text_bg, new Rectangle(w / 2 - text_bg.Width / 2, 480, text_bg.Width, text_bg.Height / 2), Color.White * alpha);
      sprite_batch.Draw(text_bg, new Rectangle(w / 2 - text_bg.Width / 4, (int)(op3_arr[0].Position.Y + (arrow.Height - text_bg.Height / 2.0f) / 2.0f), text_bg.Width / 2, text_bg.Height / 2), Color.White * alpha);

      sprite_batch.DrawString(font16, Strings.Skirmish_op3,
          new Vector2(w / 2.0f - font16.MeasureString(Strings.Skirmish_op3).X / 2.0f, 480 + (text_bg.Height / 2.0f - font16.MeasureString(Strings.Skirmish_op3).Y) / 2.0f),
          Color.White * alpha);

      sprite_batch.DrawString(font16, getBossOpt(),
          new Vector2(w / 2.0f - font16.MeasureString(getBossOpt()).X / 2.0f,
              (int)(op3_arr[0].Position.Y + (arrow.Height - text_bg.Height / 2.0f) / 2.0f) + (text_bg.Height / 2.0f - font16.MeasureString(getBossOpt()).Y) / 2.0f),
          Color.White * alpha);

      sprite_batch.Draw(arrow, op3_arr[0].Position, Color.White * alpha);
      sprite_batch.Draw(arrow, op3_arr[1].Position, null, Color.White * alpha, 0, Vector2.Zero, 1, SpriteEffects.FlipHorizontally, 0);

      sprite_batch.Draw(confirm_v, confirm.Position, Color.White * alpha);
      sprite_batch.Draw(confirm_v, back.Position, null, Color.White * alpha, 0, Vector2.Zero, 1, SpriteEffects.FlipHorizontally, 0);

      sprite_batch.End();

      base.Draw(gameTime);
    }

    string Easy = "Easy";
    string Medium = "Medium";
    string Hard = "Hard";
    private string getDifficult()
    {
      switch (difficulty)
      {
        case 0:
          return Easy;
        case 1:
          return Medium;
        case 2:
          return Hard;
      }

      return "";
    }

    private string getBossOpt()
    {
      switch (boss)
      {
        case false:
          return "No";
        case true:
          return "Yes";
      }

      return "";
    }

    private Button[] CreateArrowButton(int starting_y)
    {
      Button[] arrows = new Button[2];

      arrows[0] = new Button { Label = "", CollisionBox = new Rectangle(85, starting_y - 10, arrow.Width, arrow.Height), Position = new Vector2(95, starting_y) };
      arrows[1] = new Button { Label = "", CollisionBox = new Rectangle(345, starting_y - 10, arrow.Width, arrow.Height), Position = new Vector2(355, starting_y) };

      return arrows;
    }

    ContentManager Content { get { return Game.Content; } }
  }

}
